import numpy as np

def find_blank(phase, cell):
    """找出包含cell的成片的空格"""

    def _find_blank(phase, result, cell):
        i, j = cell
        phase[i, j] = 9
        result['cross'].add(cell)

        if i - 1 > -1:
            if phase[i - 1, j] == 0:
                _find_blank(phase, result, (i - 1, j))
            elif phase[i - 1, j] == 1:
                result['b_around'].add((i - 1, j))
            elif phase[i - 1, j] == -1:
                result['w_around'].add((i - 1, j))
        if i + 1 < 19:
            if phase[i + 1, j] == 0:
                _find_blank(phase, result, (i + 1, j))
            elif phase[i + 1, j] == 1:
                result['b_around'].add((i + 1, j))
            elif phase[i + 1, j] == -1:
                result['w_around'].add((i + 1, j))
        if j - 1 > -1:
            if phase[i, j - 1] == 0:
                _find_blank(phase, result, (i, j - 1))
            elif phase[i, j - 1] == 1:
                result['b_around'].add((i, j - 1))
            elif phase[i, j - 1] == -1:
                result['w_around'].add((i, j - 1))
        if j + 1 < 19:
            if phase[i, j + 1] == 0:
                _find_blank(phase, result, (i, j + 1))
            elif phase[i, j + 1] == 1:
                result['b_around'].add((i, j + 1))
            elif phase[i, j + 1] == -1:
                result['w_around'].add((i, j + 1))

    result = {'cross': set(), 'b_around': set(), 'w_around': set()}
    _find_blank(phase, result, cell)

    return result


def find_blanks(phase):
    """找出所有成片的空格"""

    blanks = list()
    while True:
        cells = np.where(phase == 0)
        if cells[0].size == 0:
            break
        blanks.append(find_blank(phase, (cells[0][0], cells[1][0])))

    return blanks


def stats(phase):
    """统计结果"""

    temp = np.copy(phase)
    for item in find_blanks(np.copy(phase)):
        if len(item['w_around']) == 0:
            v = 3  # 黑空
        elif len(item['b_around']) == 0:
            v = 4  # 白空
        else:
            v = 9  # 单官或公气

        for i, j in item['cross']:
            temp[i, j] = v
    print("temp->", temp)

    black = temp[temp == 1].size + temp[temp == 3].size
    white = temp[temp == -1].size + temp[temp == 4].size
    common = temp[temp == 9].size

    return black, white, common


if __name__ == '__main__':
    phase = np.array([
        [0, 0, -1, 1, 1, 0, 1, 1, 1, -1, 0, -1, 0, -1, 1, 0, 1, 0, 0],
        [0, 0, -1, 1, 0, 1, 1, 1, -1, 0, -1, 0, -1, -1, 1, 1, 1, 0, 0],
        [0, 0, -1, 1, 1, 0, 0, 1, -1, -1, 0, -1, 0, -1, 1, 0, 1, 0, 0],
        [0, -1, 1, 0, 1, 1, 0, 1, -1, 0, -1, -1, -1, 0, -1, 1, 0, 1, 0],
        [0, -1, 1, 1, 0, 1, 1, -1, -1, -1, -1, 0, 0, -1, -1, 1, 0, 1, 0],
        [0, 0, -1, 1, 1, 1, 1, -1, 0, -1, 0, -1, 0, 0, -1, 1, 0, 0, 0],
        [0, 0, -1, -1, -1, -1, 1, -1, -1, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0],
        [-1, -1, -1, 0, 0, 0, -1, 1, 1, -1, 0, -1, 0, 0, -1, 1, 0, 0, 0],
        [1, 1, -1, 0, 0, 0, -1, -1, 1, -1, 0, 0, 0, 0, -1, 1, 0, 0, 0],
        [1, 0, 1, -1, 0, -1, 1, 1, 1, 1, -1, -1, -1, 0, -1, 1, 1, 1, 1],
        [0, 1, 1, -1, 0, -1, 1, 0, 0, 0, 1, -1, 0, -1, -1, 1, 0, 0, 1],
        [1, 1, -1, -1, -1, -1, -1, 1, 0, 0, 1, -1, -1, 0, -1, 1, 0, 0, 0],
        [-1, -1, 0, -1, -1, 0, -1, 1, 0, 0, 1, -1, 0, -1, -1, -1, 1, 0, 0],
        [0, -1, 0, 0, 0, 0, -1, 1, 0, 1, 1, -1, -1, 0, -1, 1, 0, 0, 0],
        [0, -1, 0, 0, 0, -1, 1, 0, 0, 1, 0, 1, 1, -1, -1, 1, 0, 0, 0],
        [0, 0, -1, 0, -1, -1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0],
        [0, -1, -1, 0, -1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
        [0, 0, -1, 0, -1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0],
        [0, 0, 0, -1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
    ], dtype=np.ubyte)

    black, white, common = stats(phase)
    print('--------------------------------------')
    print('黑方：%d，白方：%d，公气：%d' % (black, white, common))
